<?php

namespace App\Services\MongoAI;

class Model extends \Jenssegers\Mongodb\Eloquent\Model
{

	protected $connection = 'mongo';
	protected $table = 'ai';
	public $timestamps = false;

	protected $fillable = [
		'tb',
		'count',
	];


	private static $cache;

	private static function cache ($tb)
	{
		self::$cache[$tb] = @self::$cache[$tb] ?: Model::where('tb', $tb)->first();
		if (!self::$cache[$tb]) {
			self::$cache[$tb] = new static();
			self::$cache[$tb]->fill([
				'tb' => $tb,
				'count' => \DB::connection(self::$cache[$tb]->connection)->table($tb)->count()
			]);
		}
		return self::$cache[$tb];
	}



	/**
	 * 计算Mongo表的记数
	 * @param $tb string
	 * */
	static function ai ($tb)
	{
		$obj = self::cache($tb);
		$obj->count++;
		$obj->save();
		return $obj->count;
	}



}
